package com.nononsenseapps.feeder.ui.common.video;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import com.nononsenseapps.feeder.ui.R;
import com.nononsenseapps.feeder.util.ScreenRotateUtils;
import cn.jzvd.Jzvd;
import cn.jzvd.JzvdStd;

public class GPVideoPlayerActivity extends AppCompatActivity implements ScreenRotateUtils.OrientationChangeListener{
    JzvdStd mJzvdStd;
    String url;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gpvideoplayer);

        url=getIntent().getStringExtra("url");
        String title=getIntent().getStringExtra("title");
        if(title==null)
            title=" ";

        mJzvdStd = findViewById(R.id.jz_video);
        mJzvdStd.setUp(url, title);

        String imageUrl=getIntent().getStringExtra("imageUrl");
        //if(imageUrl!=null)
            //mJzvdStd.posterImageView(imageUrl);
        ScreenRotateUtils.getInstance(this.getApplicationContext()).setOrientationChangeListener(this);
        mJzvdStd.startVideo();
    }

    @Override
    public void onBackPressed() {
        if (Jzvd.backPress()) {
            return;
        }
        super.onBackPressed();
    }
    @Override
    protected void onPause() {
        super.onPause();
        Jzvd.releaseAllVideos();
        ScreenRotateUtils.getInstance(this).stop();
    }

    @Override
    protected void onResume() {
        super.onResume();
        ScreenRotateUtils.getInstance(this).start(this);
    }

    @Override
    protected void onDestroy() {
        ScreenRotateUtils.getInstance(this).setOrientationChangeListener(null);
        super.onDestroy();
    }

    @Override
    public void orientationChange(int orientation) {
        if (Jzvd.CURRENT_JZVD != null
                && (mJzvdStd.state == Jzvd.STATE_PLAYING || mJzvdStd.state == Jzvd.STATE_PAUSE)
                && mJzvdStd.screen != Jzvd.SCREEN_TINY) {
            if (orientation >= 45 && orientation <= 315 && mJzvdStd.screen == Jzvd.SCREEN_NORMAL) {
                changeScreenFullLandscape(ScreenRotateUtils.orientationDirection);
            } else if (((orientation >= 0 && orientation < 45) || orientation > 315) && mJzvdStd.screen == Jzvd.SCREEN_FULLSCREEN) {
                changeScrenNormal();
            }
        }
    }

    /**
     * 竖屏并退出全屏
     */
    private void changeScrenNormal() {
        if (mJzvdStd != null && mJzvdStd.screen == Jzvd.SCREEN_FULLSCREEN) {
            mJzvdStd.autoQuitFullscreen();
        }
    }

    /**
     * 横屏
     */
    private void changeScreenFullLandscape(float x) {
        //从竖屏状态进入横屏
        if (mJzvdStd != null && mJzvdStd.screen != Jzvd.SCREEN_FULLSCREEN) {
            if ((System.currentTimeMillis() - Jzvd.lastAutoFullscreenTime) > 2000) {
                mJzvdStd.autoFullscreen(x);
                Jzvd.lastAutoFullscreenTime = System.currentTimeMillis();
            }
        }
    }

    /***
     * 启动播放视频
     * @param   context context
     * @param  url url
     * @param title title
     * @param imageUrl image url***/
    public static  void startActivity(Context context, String url, String title, String imageUrl){
        Intent intent=new Intent(context, GPVideoPlayerActivity.class);
        intent.putExtra("url",url);
        if(title!=null&& !title.isEmpty())
            intent.putExtra("title",title);
        if(imageUrl!=null&& !imageUrl.isEmpty())
            intent.putExtra("imageUrl",imageUrl);
        context.startActivity(intent);
    }
}
